/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics3D.gui;

import czStvDpds.input.LWJGLKeyEvent;
import java.util.ArrayList;

/**
 * Container, do ktereho je mozno pridavat komponenty
 *
 * @author Ondra
 */
public class LGridPanel extends LComponent implements LGridContainer {

    private int columns, rows;
    private ArrayList<LComponent> components;
    private int componentCount;
    private ComponentPixelSize cellSize;
    private ComponentRelativeSize panelSize;

    public LGridPanel(int rows, int columns) {
        super();
        componentCount = rows * columns;
        components = new ArrayList();
        cellSize = new ComponentPixelSize(0, 0);
        panelSize = new ComponentRelativeSize(0f, 0f);

        this.rows = rows;
        this.columns = columns;
    }

    @Override
    public void addComponent(LComponent component) {
        if (componentCount > components.size()) {
            components.add(component);
            component.setParent(this);
        }
        recalculate();

//        recalculateMinSize();
    }

    @Override
    public void removeComponent(LComponent component) {
        components.remove(component);
        recalculate();
    }

    @Override
    public int getRows() {
        return rows;
    }

    @Override
    public int getColumns() {
        return columns;
    }

    private void recalculate() {
        ComponentPixelSize oldCellSize = new ComponentPixelSize(cellSize.getHeight(), cellSize.getWidth());
        for (LComponent comp : components) {
            cellSize = cellSize.max(comp.getMinSize());
        }
        if (!cellSize.equals(oldCellSize)) {
            recalculateMinSize();
            if (parent != null) {
                parent.childChanged(this);
            }
        }
    }

    /**
     * Podle nastavenych parametru zadava detem pozice a velikosti.
     */
    private void assignBounds() {
        float compWidth = panelSize.getWidth() / columns;
        float compHeight = panelSize.getHeight() / rows;
        float firstX = loc.getX() + ((panelSize.getWidth() - (columns * compWidth)) / 2);
        float firstY = loc.getY() + ((panelSize.getHeight() - (rows * compHeight)) / 2);

        int row = 0, column = 0;
        for (LComponent comp : components) {
            comp.setBounds(firstX + column * compWidth, firstY + row * compHeight, compWidth, compHeight);
            column++;
            if (column >= columns) {
                row++;
                column = 0;
            }
        }
    }

    @Override
    public void childChanged(LComponent child) {
        recalculate();
    }

    @Override
    public void setVisible(boolean visible) {
        for (LComponent comp : components) {
            comp.setVisible(visible);
        }
    }

    @Override
    public void setBounds(float x, float y, float width, float height) {
        super.setBounds(x, y, width, height);

        panelSize.setHeight(height);
        panelSize.setWidth(width);

        //kontrola jestli se do novych rozmeru vejdou bunky
        int reducedWidth = Modeler.ratioToPixels(panelSize.getWidth());
        int reducedHeight = Modeler.ratioToPixels(panelSize.getHeight());

        if (reducedWidth / columns < cellSize.getWidth()) {
            //ne
            reducedWidth = Modeler.ratioToPixels(panelSize.getWidth());
            reducedHeight = Modeler.ratioToPixels(panelSize.getHeight());

            cellSize.setHeight(reducedHeight / rows);
            cellSize.setWidth(reducedWidth / columns);
        }

        if (reducedHeight / rows < cellSize.getHeight()) {
            //ne
            reducedWidth = Modeler.ratioToPixels(panelSize.getWidth());
            reducedHeight = Modeler.ratioToPixels(panelSize.getHeight());

            cellSize.setHeight(reducedHeight / rows);
            cellSize.setWidth(reducedWidth / columns);
        }

        assignBounds();
        recalculateMinSize();
    }

    @Override
    protected void recalculateMinSize() {
        ComponentPixelSize minGridSize = new ComponentPixelSize(cellSize.getHeight() * rows, cellSize.getWidth() * columns);
        this.setMinSize(minGridSize);
    }

    @Override
    protected void model() {
    }

    @Override
    protected void mouseExited() {
    }

    @Override
    protected void mouseEntered() {
    }

    @Override
    protected void mousePressed() {
    }

    @Override
    protected void mouseReleased() {
    }

    @Override
    protected void keyPressed(LWJGLKeyEvent e) {
    }

    @Override
    protected void mousePressedNotHere() {
    }
}
